package cn.tansanqinger.dao.sql.catalogue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.ICatalogueSqlDao;
import cn.tansanqinger.pojo.Catalogue;

@Repository
public class CatalogueSqlDaoImpl {
	
	@Autowired
	private ICatalogueSqlDao dao;
	
	/**
	 * 查询数据，通过条件查询
	 * @param grade
	 * @param keywords
	 * @param page
	 * @param limit
	 * @return
	 */
	public Map<String, Object> selectMessage(String keywords, int ispopup, int page, int limit){
		QueryWrapper<Catalogue> queryWrapper = new QueryWrapper<Catalogue>();
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("cno", keywords).or().like("cname", keywords).or().like("ctop", keywords));
		}
		if(ispopup!=-1) {
			queryWrapper.lambda().eq(Catalogue::getIspopup, ispopup);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", dao.selectCount(queryWrapper));
		Page<Catalogue> pg = new Page<Catalogue>(page, limit);
		IPage<Catalogue> iPage = dao.selectPage(pg, queryWrapper);
		List<Catalogue> coursesList = iPage.getRecords();
		resultMap.put("data", coursesList);
		resultMap.put("code","0");
		resultMap.put("msg", "查询成功");
		return resultMap;
	}
	
	public List<Catalogue> selectMenu(int ctop) {
		QueryWrapper<Catalogue> queryWrapper = new QueryWrapper<Catalogue>();
		if(ctop==0) {
			queryWrapper.lambda().eq(Catalogue::getCtier, 1);
		} else {
			queryWrapper.lambda().eq(Catalogue::getCtop, ctop);
		}
		return dao.selectList(queryWrapper);
	}
	
	/**
	 * 查询数据，查询全部数据
	 * @return
	 */
	public List<Catalogue> selectMessageAll(){
		return dao.selectList(null);
	}
	
	/**
	 * 获取编号
	 * @return
	 */
	public Integer serialNumber(int ctier) {
		QueryWrapper<Catalogue> queryWrapper = new QueryWrapper<Catalogue>();
		queryWrapper.lambda().eq(Catalogue::getCtier, ctier);
		queryWrapper.orderByDesc("cno").last("limit 1");
		Catalogue object= dao.selectOne(queryWrapper);
		if(ctier==1) {
			return object==null?10000001:object.getCno()+1;
		} else if (ctier==2) {
			return object==null?20000001:object.getCno()+1;
		} else if (ctier==3) {
			return object==null?30000001:object.getCno()+1;
		}
		return -1;
	}
	
	/**
	 * 添加信息
	 * @param course
	 * @return
	 */
	public int add(Catalogue object) {
		return dao.insert(object);
	}
	
	/**
	 * 查询信息
	 * @param cid
	 * @return
	 */
	public Catalogue selectOnt(int id) {
		return dao.selectById(id);
	}
	
	/**
	 * 修改状态
	 * @param cid
	 * @param isuse
	 * @return
	 */
	public int updateStatus(Catalogue object) {
		return dao.updateById(object);
	}
	
	/**
	 * 删除信息
	 * @param cid
	 * @return
	 */
	public int deleteOne(int id, int sn) {
		QueryWrapper<Catalogue> queryWrapper = new QueryWrapper<Catalogue>();
		queryWrapper.lambda().eq(Catalogue::getCid, id).eq(Catalogue::getCno, sn);
		return dao.delete(queryWrapper);
	}
	
	/**
	 * 修改信息
	 * @param user
	 * @return
	 */
	public int modification(Catalogue object) {
		return dao.updateById(object);
	}

}
